package pw.hais.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 接收信息处理
 * Created by hais1992 on 2016/7/5/005.
 */
public class ClientHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String str) throws Exception {
        NettyLog.e("收到信息：" + str);
        if (!NettyClient.keepaliveFlag.equals(str)) {
            NettyEventListener listener = NettyClient.getInstance().listener;
            if (listener != null) listener.onEventMessage(str);
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
//        super.channelInactive(ctx);
        NettyClient.getInstance().loopConnect(ctx.channel(), new Exception("网络不稳定，和服务器连接失败！"));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        super.exceptionCaught(ctx, cause);
        NettyClient.getInstance().isConnections = true; //-.-Bug？
        NettyClient.getInstance().loopConnect(ctx.channel(),new Exception("网络不稳定，和服务器连接中断！"));
    }
}
